package com.cskaoyan.mall.controller.admin;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.mall.model.*;
import com.cskaoyan.mall.model.admin.AdminLoginBO;
import com.cskaoyan.mall.model.admin.AdminLoginVO;
import com.cskaoyan.mall.model.admin.AdminVO;
import com.cskaoyan.mall.service.AdminServiceImpl;
import com.cskaoyan.mall.service.AdminService;
import com.cskaoyan.mall.util.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import static com.cskaoyan.mall.util.RequestBodyUtils.getRequestBody;
import static com.cskaoyan.mall.util.UpdateOrAddAdminUtils.*;

/**
 * @author: jyf2000jyf
 */
@WebServlet("/api/admin/admin/*")
public class AdminServlet extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/admin/admin/", "");
        if ("login".equals(op)) {
            login(request, response);
        } else if ("updateAdminss".equals(op)) {
            updateAdminss(request, response);
        } else if ("getSearchAdmins".equals(op)) {
            getSearchAdmins(request, response);
        } else if ("changePwd".equals(op)) {
            changePwd(request, response);
        } else if ("addAdminss".equals(op)) {
            addAdminss(request, response);
        } else if ("logoutAdmin".equals(op)) {
            logoutAdmin(request, response);
        }
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1.获取请求参数:自行解析请求体数据
        String requestBody = getRequestBody(request);
        // 请求体 json字符串------java对象  {"email":"admin","pwd":"admin123"}
        AdminLoginBO adminLoginBO = gson.fromJson(requestBody, AdminLoginBO.class);
        // 校验
        if (StringUtils.isEmpty(adminLoginBO.getEmail()) || StringUtils.isEmpty(adminLoginBO.getPwd())) {
            Result result = new Result(10000, "用户名、密码不能为空", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        int code = adminService.login(adminLoginBO);
        if (code == 200) {
            HttpSession session = request.getSession();
            session.setAttribute("email", adminLoginBO.getEmail());
            //登录成功  {"code":0,"data":{"token":"admin","name":"admin"}}
            Result result = new Result(0, null, new AdminLoginVO(adminLoginBO.getEmail(), adminLoginBO.getPwd()));
            response.getWriter().println(gson.toJson(result));
            return;
        } else if (code == 404) {
            Result result = new Result(10000, "密码不正确", null);
            response.getWriter().println(gson.toJson(result));
            return;
        } else {
            Result result = new Result(10000, "服务器繁忙", null);
            response.getWriter().println(gson.toJson(result));
        }
    }

    private void updateAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = getRequestBody(request);
        AdminVO adminVO = gson.fromJson(requestBody, AdminVO.class);
        Result result;
        // 5-12位qq号 + @qq.com
        if (!judgeEmail(adminVO.getEmail())) {
            result = new Result(10000, "账户正则验证未通过", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        // 4-15位昵称
        if (!judgeNickname(adminVO.getNickname())) {
            result = new Result(10000, "昵称正则验证未通过", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        // 8-16位，包括大小写字母、数字和符号
        if (!judgePassword(adminVO.getPwd())) {
            result = new Result(10000, "密码正则验证未通过", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        boolean status = adminService.updateAdminss(adminVO);
        if (status) {
            result = new Result(0, null, null);
            response.getWriter().println(gson.toJson(result));
            return;
        } else {
            result = new Result(10000, "修改失败", null);
            response.getWriter().println(gson.toJson(result));
        }
    }

    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = getRequestBody(request);
        AdminVO AdminVO = gson.fromJson(requestBody, AdminVO.class);
        List<AdminVO> adminVOList = adminService.getSearchAdmins(AdminVO);
        Result result = new Result(0, null, adminVOList);
        response.getWriter().println(gson.toJson(result));
    }

    private void addAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = getRequestBody(request);
        AdminVO adminVO = gson.fromJson(requestBody, AdminVO.class);
        Result result;
        // 5-12位qq号 + @qq.com
        if (!judgeEmail(adminVO.getEmail())) {
            result = new Result(10000, "账户正则验证未通过", null);
            response.getWriter().println(gson.toJson(result));
        }
        // 4-15位昵称
        if (!judgeNickname(adminVO.getNickname())) {
            result = new Result(10000, "昵称正则验证未通过", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        // 8-16位，包括大小写字母、数字和符号
        if (!judgePassword(adminVO.getPwd())) {
            result = new Result(10000, "密码正则验证未通过", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        boolean status = adminService.addAdminss(adminVO);
        if (status == true) {
            result = new Result(0, "插入成功", null);
        } else {
            result = new Result(10000, "插入失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void changePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = getRequestBody(request);
        ChangePwd Pwd = gson.fromJson(requestBody, ChangePwd.class);
        Result result;
        String returnPwd = adminService.changePwd(Pwd);
        if (returnPwd == null) {
            result = new Result(10000, "密码为空", null);
            response.getWriter().println(gson.toJson(result));
            return;
        }
        if ("确认密码和新密码不一致".equals(returnPwd) || "sql异常".equals(returnPwd) || "原密码错误".equals(returnPwd) || "密码正则验证未通过".equals(returnPwd)) {
            result = new Result(10000, returnPwd, null);
            response.getWriter().println(gson.toJson(result));
            return;
        }

        result = new Result(0, "修改密码成功", null);
        response.getWriter().println(gson.toJson(result));
    }

    private void logoutAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        Result result;
        if (session.getAttribute("email") != null) {
            session.invalidate();
            result = new Result(0, "注销成功", null);
        } else {
            result = new Result(10000,"注销失败",null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace("/api/admin/admin/", "");
        if ("allAdmins".equals(op)) {
            getAllAdmins(request, response);
        } else if ("deleteAdmins".equals(op)) {
            deleteAdmins(request, response);
        } else if ("getAdminsInfo".equals(op)) {
            getAdminsInfo(request, response);
        }
    }

    private void getAllAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Result result = new Result(0, null, adminService.getAllAdmins());
        response.getWriter().println(gson.toJson(result));
    }

    private void deleteAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean status = adminService.deleteAdmins(id);
        Result result;
        if (status) {
            result = new Result(0, "删除成功", null);
        } else {
            result = new Result(10000, "删除失败", null);
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void getAdminsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        AdminVO adminVO = adminService.getAdminsInfo(id);
        Result result;
        if (adminVO != null) {
            result = new Result(0, null, adminService.getAdminsInfo(id));
        } else {
            result = new Result(10000, "修改出错！", null);
        }
        response.getWriter().println(gson.toJson(result));
    }
}
